package com.wframe.project.former.dataservices.order;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.frame.database.JBaseDao;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.frame.util.DateConvert;
import com.wframe.project.former.dataservices.products.dao.ProductClassDao;
import com.wframe.project.former.entity.ProductsAttrvalBean;
import com.wframe.project.former.enums.OrderStatusEnum;

public class OrderDao extends JBaseDao{

	public OrderDao() {
		super("wb_order_list");
	}

	public PageResult queryOrder(JdataBean parms,int size,int pageno) throws Exception{
		String sql = "select l.*,from_unixtime(order_time) ordertime,u.loginname username,c.`name` name from wb_order_list l,wbuser u,wb_company c where l.userid=u.id and l.userid=c.userid and l.backtag=0 and l.status>0";
		List parmlist = new ArrayList();
		String order = "";
		if (parms != null) {
			if (StringUtils.isNotBlank(parms.getString("paytype"))) {
				sql += " and l.paytype=?";
				parmlist.add(parms.getString("paytype"));
			}
			if (StringUtils.isNotBlank(parms.getString("code"))) {
				sql += " and l.code like ?";
				parmlist.add("%" + parms.getString("code") + "%");
			}
			if (StringUtils.isNotBlank(parms.getString("loginname"))) {
				sql += " and u.loginname like ?";
				parmlist.add("%" + parms.getString("loginname") + "%");
			}
			if (StringUtils.isNotBlank(parms.getString("name"))) {
				sql += " and c.name like ?";
				parmlist.add("%" + parms.getString("name") + "%");
			}
			if (StringUtils.isNotBlank(parms.getString("status"))) {
				sql += " and l.status=?";
				parmlist.add(parms.getString("status"));
			}
			if (StringUtils.isNotBlank(parms.getString("start"))) {
				sql += " and l.order_time>=?";
				parmlist.add(DateConvert.toDate(parms.getString("start")).getTime()/1000);
			}
			if (StringUtils.isNotBlank(parms.getString("end"))) {
				sql += " and l.order_time<=?";
				parmlist.add(DateConvert.toDate(parms.getString("end")+" 23:59:59").getTime()/1000);
			}
		}
		sql += " order by l.order_time desc";
		return getSession().query2(sql, parmlist, size, pageno);
	}

	public PageResult queryBackOrder(JdataBean parms,int size,int pageno) throws Exception{
		String sql = "select l.*,b.phone backphone,b.applytime,from_unixtime(order_time) ordertime,u.title username,(select sum(p.price*p.amount) from wb_order_backproduct p where p.orderid=l.id) summoney from wb_order_list l,wbuser u,wb_order_back b where l.userid=u.id and l.id=b.orderid";
		List parmlist = new ArrayList();
		String order = "";
		if (parms != null) {
			if (StringUtils.isNotBlank(parms.getString("code"))) {
				sql += " and l.code like ?";
				parmlist.add("%" + parms.getString("code") + "%");
			}
			if (StringUtils.isNotBlank(parms.getString("start"))) {
				sql += " and b.applytime>=?";
				parmlist.add(parms.getString("start"));
			}
			if (StringUtils.isNotBlank(parms.getString("end"))) {
				sql += " and b.applytime<=?";
				parmlist.add(parms.getString("end")+" 23:59:59");
			}
			if(StringUtils.isNotBlank(parms.getString("backtag"))){
				sql+=" and l.backtag=?";
				parmlist.add(parms.getString("backtag"));
			}
		}
		sql += " order by b.applytime desc";
		return getSession().query2(sql, parmlist, size, pageno);
	}

	public JdataBean getOrder(String id) throws Exception{
		return getSession().getObject("select l.*,from_unixtime(order_time) ordertime,from_unixtime(send_time) sendtime,u.title username,u.phone userphone,u.loginname from wb_order_list l,wbuser u where l.userid=u.id and l.id=?", new Object[]{id});
	}

	public List<JdataBean> queryOrderProduct(String orderid) throws Exception{
		String sql="select c.amount buyamount,c.price buyprice,c.price*c.amount sumprice,t.*,p.code productcode,p.name productname,p.unitname,p.attrvals,s.filepath photourl,s.viewpath photoview,sm.filepath product_photourl,sm.viewpath product_photoview"
				+ " from wb_order_product c"
				+" inner join wb_products_prices t on t.id=c.priceid"
				+" inner join wb_products p on p.id=c.productid"
				+" left join app_pictures s on s.id=t.photo"//销售属性主图片
				+" left join app_pictures sm on sm.id=p.photo"//商品主图片
				+" where  c.orderid =? order by c.gift asc";
		List<JdataBean>  productlist=getSession().query(sql, new Object[]{orderid});
		for(JdataBean product:productlist){
			String photo=product.get("photoview")==null?"":product.get("photoview").toString();
			if(StringUtils.isBlank(photo))photo=product.get("product_photoview")==null?"":product.get("product_photoview").toString();
			product.put("photo", photo);
			String attrjson=product.get("attrvals")==null?"":product.get("attrvals").toString();
			if(StringUtils.isNotBlank(attrjson)){
				//将商品属性取出
				List<ProductsAttrvalBean> jsonarr=JSON.parseArray(attrjson,ProductsAttrvalBean.class);
				for(ProductsAttrvalBean attrbean:jsonarr){
					product.put(attrbean.getAttrcode(),attrbean.getValtxt());
				}
			}
		}
		return productlist;
	}
	public List<JdataBean> queryBackOrderProduct(String orderid) throws Exception{
		String sql="select c.amount backamount,c.price buyprice,c.priceid,c.price*c.amount sumprice,c.attrnames,c.productname,p.code productcode,p.unitname,p.attrvals,s.filepath photourl,s.viewpath photoview,sm.filepath product_photourl,sm.viewpath product_photoview"
				+ " from wb_order_backproduct c"
				+" inner join wb_products_prices t on t.id=c.priceid"
				+" inner join wb_products p on p.id=c.productid"
				+" left join app_pictures s on s.id=t.photo"//销售属性主图片
				+" left join app_pictures sm on sm.id=p.photo"//商品主图片
				+" where c.backid in (select id from wb_order_back where orderid=?)";
		List<JdataBean>  productlist=getSession().query(sql, new Object[]{orderid});
		for(JdataBean product:productlist){
			String photo=product.get("photoview")==null?"":product.get("photoview").toString();
			if(StringUtils.isBlank(photo))photo=product.get("product_photoview")==null?"":product.get("product_photoview").toString();
			product.put("photo", photo);
			String attrjson=product.get("attrvals")==null?"":product.get("attrvals").toString();
			if(StringUtils.isNotBlank(attrjson)){
				//将商品属性取出
				List<ProductsAttrvalBean> jsonarr=JSON.parseArray(attrjson,ProductsAttrvalBean.class);
				for(ProductsAttrvalBean attrbean:jsonarr){
					product.put(attrbean.getAttrcode(),attrbean.getValtxt());
				}
			}
		}
		return productlist;
	}

	public void fahuo(String orderid,String send_time,String expres_type,String expres_code) throws Exception{
		getSession().update("update wb_order_list set expres_type=?,expres_code=?,send_time=?,status=? where id=?",new Object[]{expres_type,expres_code,(DateConvert.toDate(send_time).getTime()/1000),OrderStatusEnum.Sended.getStatus(),orderid});
	}
	public void updateorderBackTag(String orderid,int status) throws Exception{
		getSession().update("update wb_order_list set backtag=? where id=?",new Object[]{status,orderid});
	}
	public JdataBean getBackOrder(String orderid) throws Exception{
		return getSession().getObject("select l.* from wb_order_back l where orderid=?", new Object[]{orderid});
	}
	public void backorderauth(String orderid,int status,String authcon) throws Exception{
		getSession().update("update wb_order_back set status=?,authtime=now(),authcon=? where orderid=?",new Object[]{status,authcon,orderid});
	}
}
